def handleGroovyScript(gscript) {
	def shell = session.getAttribute("groovyShell")

	if(shell == null) {
		def binding = new Binding()

		// Spring WebApplicationContext binding
		//def appCtx = org.springframework.web.context.support.WebApplicationContextUtils.getWebApplicationContext(context)
		//binding.setVariable("appCtx", appCtx)

		binding.setVariable("request", request)
		binding.setVariable("response", response)
		binding.setVariable("context", context)
		binding.setVariable("session", session)

		shell = new GroovyShell(binding)
		session.setAttribute("groovyShell", shell)
	}

	shell.evaluate(gscript)
}

def results

try {
	def gscript = request.getParameter('gscript')
	
	if(gscript == null || gscript.trim() == "") {
		throw new RuntimeException("No script was sent!")
	}
	
	results = handleGroovyScript(gscript)
}
catch(e) {
	results = e.toString()
}

println results